package com.example.demo.controller;

import com.example.demo.entity.Student;
import com.example.demo.service.Student.StudentService;


import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController//返回的值是json
@CrossOrigin(allowedHeaders = "*")//允许跨域
public class StudentController {
    @Resource
    private StudentService studentService;

    //访问http://localhost:8080/queryStudent会跳转到该函数
    @GetMapping("/queryStudent")
    public List<Student> getStudent() {
        return studentService.queryStudentList();
    }

    @GetMapping("/getStudentByName/{name}")
    public List<Student> getStudentByName(@PathVariable String name) {

        return studentService.queryStudentByName(name);
    }

    @GetMapping("/getStudentByMajor/{major}")
    public List<Student> getStudentByMajor(@PathVariable String major) {
        return studentService.queryStudentByMajor(major);
    }

    @PostMapping("/addStudent")
    public String addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        return "插入成功";
    }

    @GetMapping("/deleteStudent/{id}")
    public String deleteStudent(@PathVariable int id) {
        studentService.deleteStudent(id);
        return "删除成功";
    }

    @PostMapping(value = "/updateStudent")
    public String updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        return "修改成功";
    }

    @GetMapping("/getStudentById/{id}")
    public List<Student> getStudentById(@PathVariable int id) {
        return studentService.queryStudentById(id);
    }


}
